//-----------------------------------------------------------------------
// <copyright file="PictureDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using .Entities;

using Pyco;
using Pyco.Diagnostics;
using Pyco.Data;

namespace .Data
{    
    public abstract class PictureDAOBase
    {
        #region Common methods
        public virtual Picture CreatePictureFromReader(IDataReader reader)
        {
            Picture item = new Picture();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("PictureId"))) item.PictureId = (long)reader["PictureId"];
                if (!reader.IsDBNull(reader.GetOrdinal("UserId"))) item.UserId = (long)reader["UserId"];
                if (!reader.IsDBNull(reader.GetOrdinal("CountryLanguageId"))) item.CountryLanguageId = (int)reader["CountryLanguageId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) item.Title = (string)reader["Title"];
                if (!reader.IsDBNull(reader.GetOrdinal("Description"))) item.Description = (string)reader["Description"];
                if (!reader.IsDBNull(reader.GetOrdinal("FileName"))) item.FileName = (string)reader["FileName"];
                if (!reader.IsDBNull(reader.GetOrdinal("Status"))) item.Status = (int)reader["Status"];
                if (!reader.IsDBNull(reader.GetOrdinal("Type"))) item.Type = (int)reader["Type"];
                if (!reader.IsDBNull(reader.GetOrdinal("Votes"))) item.Votes = (int)reader["Votes"];
                if (!reader.IsDBNull(reader.GetOrdinal("TotalRating"))) item.TotalRating = (int)reader["TotalRating"];
                if (!reader.IsDBNull(reader.GetOrdinal("AverageVote"))) item.AverageVote = (decimal)reader["AverageVote"];
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) item.CreatedDate = (DateTime)reader["CreatedDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("LastModificationDate"))) item.LastModificationDate = (DateTime)reader["LastModificationDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("MailingAddress"))) item.MailingAddress = (string)reader["MailingAddress"];
                if (!reader.IsDBNull(reader.GetOrdinal("ContactNumber"))) item.ContactNumber = (string)reader["ContactNumber"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreatePictureFromReaderException, ex);
            }
            return item;
        }
        #endregion
		
        #region CreatePicture methods
            
        public virtual void CreatePicture(Picture picture)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spPicturesInsert");
                
                database.AddInParameter(dbCommand, "@UserId", DbType.Int64, picture.UserId);
                database.AddInParameter(dbCommand, "@CountryLanguageId", DbType.Int32, picture.CountryLanguageId);
                database.AddInParameter(dbCommand, "@Title", DbType.String, picture.Title);
                database.AddInParameter(dbCommand, "@Description", DbType.String, picture.Description);
                database.AddInParameter(dbCommand, "@FileName", DbType.AnsiString, picture.FileName);
                database.AddInParameter(dbCommand, "@Status", DbType.Int32, picture.Status);
                database.AddInParameter(dbCommand, "@Type", DbType.Int32, picture.Type);
                database.AddInParameter(dbCommand, "@Votes", DbType.Int32, picture.Votes);
                database.AddInParameter(dbCommand, "@TotalRating", DbType.Int32, picture.TotalRating);
                database.AddInParameter(dbCommand, "@AverageVote", DbType.Decimal, picture.AverageVote);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, picture.CreatedDate);
                database.AddInParameter(dbCommand, "@LastModificationDate", DbType.DateTime, picture.LastModificationDate);
                database.AddInParameter(dbCommand, "@MailingAddress", DbType.String, picture.MailingAddress);
                database.AddInParameter(dbCommand, "@ContactNumber", DbType.String, picture.ContactNumber);
                database.AddOutParameter(dbCommand, "@PictureId", DbType.Int64, 0);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                picture.PictureId = (long)database.GetParameterValue(dbCommand, "@PictureId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreatePictureException, ex);
            }
        }

        #endregion
		
        #region UpdatePicture methods
        
        public virtual void UpdatePicture(Picture picture)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spPicturesUpdate");            
                
                database.AddInParameter(dbCommand, "@PictureId", DbType.Int64, picture.PictureId);
                database.AddInParameter(dbCommand, "@UserId", DbType.Int64, picture.UserId);
                database.AddInParameter(dbCommand, "@CountryLanguageId", DbType.Int32, picture.CountryLanguageId);
                database.AddInParameter(dbCommand, "@Title", DbType.String, picture.Title);
                database.AddInParameter(dbCommand, "@Description", DbType.String, picture.Description);
                database.AddInParameter(dbCommand, "@FileName", DbType.AnsiString, picture.FileName);
                database.AddInParameter(dbCommand, "@Status", DbType.Int32, picture.Status);
                database.AddInParameter(dbCommand, "@Type", DbType.Int32, picture.Type);
                database.AddInParameter(dbCommand, "@Votes", DbType.Int32, picture.Votes);
                database.AddInParameter(dbCommand, "@TotalRating", DbType.Int32, picture.TotalRating);
                database.AddInParameter(dbCommand, "@AverageVote", DbType.Decimal, picture.AverageVote);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, picture.CreatedDate);
                database.AddInParameter(dbCommand, "@LastModificationDate", DbType.DateTime, picture.LastModificationDate);
                database.AddInParameter(dbCommand, "@MailingAddress", DbType.String, picture.MailingAddress);
                database.AddInParameter(dbCommand, "@ContactNumber", DbType.String, picture.ContactNumber);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessUpdatePictureException, ex);
            }
        }
        
        #endregion
		
        #region DeletePicture methods
        public virtual void DeletePicture(long pictureId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spPicturesDelete");
                
                database.AddInParameter(dbCommand, "@PictureId", DbType.Int64, pictureId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessDeletePictureException, ex);
            }
        }

        #endregion
		
        #region GetPicture methods
        
        public virtual Picture GetPicture(long pictureId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spPicturesGet");
                
                database.AddInParameter(dbCommand, "@PictureId", DbType.Int64, pictureId);
                
                Picture picture =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        picture = CreatePictureFromReader(reader);
                        reader.Close();
                    }
                }
                return picture;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetPictureException, ex);
            }
        }
        
        #endregion
		
        #region GetPictureList methods
        public virtual PictureCollection GetPictureList(PictureColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spPicturesGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                PictureCollection pictureCollection = new PictureCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Picture picture = CreatePictureFromReader(reader);
                        pictureCollection.Add(picture);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return pictureCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetPictureListException, ex);
            }
        }
        
        public virtual PictureCollection GetPictureList(PictureColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetPictureList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
    }
}